package com.tianpengtech.admin.fairadvertisement;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.jfinal.plugin.activerecord.Model;
import com.tianpengtech.common.exception.ValidateException;
import com.tianpengtech.admin.AbstractAdmin;
import com.tianpengtech.common.model.fairadvertisement.FairAdvertisement;
import com.tianpengtech.common.search.LikeCondition;
import com.tianpengtech.common.search.QueryConstructor;
import com.tianpengtech.common.validator.Validator;
import com.tianpengtech.common.validator.rules.string.StringLengthBetween;
import com.tianpengtech.common.validator.rules.string.StringMustBeRequired;

public class FairAdvertisementAdminServiceImpl extends AbstractAdmin{

	@Override
	public void beforeAdd(Model<?> model) throws ValidateException {

		String title = model.getStr(FairAdvertisement.TITLE);
		String picture = model.getStr(FairAdvertisement.PICTURE);
		String url = model.getStr(FairAdvertisement.URL);
		
		Validator validator = new Validator();
		validator.addRule(new StringLengthBetween(title, "请输入1-50个字符的广告图片名称", 1, 50));
		validator.addRule(new StringMustBeRequired(picture, "图片不能为空"));
		validator.addRule(new StringMustBeRequired(url, "链接地址不能为空"));
		validator.excute();
		
	}

	@Override
	public void beforeQueryById(Model<?> model, int id) {

	}

	@Override
	public void beforeUpdate(Model<?> model) throws ValidateException {
		String title = model.getStr(FairAdvertisement.TITLE);
		String picture = model.getStr(FairAdvertisement.PICTURE);
		String url = model.getStr(FairAdvertisement.URL);
		Validator validator = new Validator();
		validator.addRule(new StringLengthBetween(title, "请输入1-50个字符的广告图片名称", 1, 50));
		validator.addRule(new StringMustBeRequired(picture, "图片不能为空"));
		validator.addRule(new StringMustBeRequired(url, "链接地址不能为空"));
		validator.excute();
	}

	@Override
	public void beforeDelete(Model<?> model) {

	}

	@Override
	public void builderSQLExpected(Model< ? > model,
			QueryConstructor queryConstruct, Map< String, Object > params) {
		
		String title = model.getStr(FairAdvertisement.TITLE);
		if(StringUtils.isNoneEmpty(title)){
			queryConstruct.and(new LikeCondition(FairAdvertisement.TITLE, "%"+title+"%"));
		}
		
	}

	@Override
	public String tableName() {
		return FairAdvertisement.dao.tableName();
	}

}